﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Formality.FormalSystem
{
    public class Axiom : Sentence
    {
        public static char INFERS = '\u22a2';

        public Axiom(Proposition Consequent, params Proposition[] Antecedents)
            : this(Antecedents, Consequent)
        { }

        public Axiom(IEnumerable<Proposition> Antecedents, Proposition Consequent)
        {
            this.Antecedents = Antecedents;
            this.Consequent = Consequent;
        }

        public IEnumerable<Proposition> Antecedents { get; private set; }
        public Proposition Consequent { get; private set; }

        #region Sentence overrides
        public override Inference? Implies(Proposition Goal, IEnumerable<Sentence> Given)
        {
            var Substitution = new Substitution();

            var Inference = Consequent.Matches(Goal, Substitution);

            if (Inference.HasValue)
            {
                var From = Satisfies(Antecedents.ToArray(), 0, Given, Substitution);

                if (From != null)
                    return new Inference
                    {
                        Description = string.Format("{0} {1}", DisplayId, From)
                    };
            }

            return null;
        }

        public override bool Equals(Sentence Sentence)
        {
            var that = Sentence as Axiom;

            if (that == null)
                return false;

            return Antecedents.SequenceEqual(that.Antecedents)
                && Consequent.Equals(that.Consequent);
        }

        public override int HashCode
        {
            get
            {
                var Hash = 0;

                foreach (var A in Antecedents)
                    Hash ^= A.GetHashCode();

                return Hash ^ Consequent.GetHashCode();
            }
        }

        public override void Serialize(TextWriter Writer, bool Detailed)
        {
            var AnteIter = Antecedents.GetEnumerator();

            var More = AnteIter.MoveNext();

            while (More)
            {
                AnteIter.Current.Serialize(Writer, Detailed);

                More = AnteIter.MoveNext();

                if (More)
                    Writer.Write(", ");
                else
                    Writer.Write(" ");
            }

            Writer.Write(INFERS + " ");

            Consequent.Serialize(Writer, Detailed);
        }

        public override void Bind(Bindings Bindings, int Depth)
        {
            foreach (var A in Antecedents)
                A.Bind(Bindings, Depth);
            Consequent.Bind(Bindings, Depth);
        }
        #endregion

        private Substitution Satisfies(Proposition[] Antecedents, int AnteIter, IEnumerable<Sentence> Given, Substitution Substitution)
        {
            if (Antecedents.Length == AnteIter)
                return Substitution;

            foreach (var G in from S in Given
                              where S is Proposition
                              select S as Proposition)
            {
                var NextSubstitution = new Substitution(Substitution);

                var Inference = Antecedents[AnteIter].Matches(G, NextSubstitution);

                if (Inference.HasValue)
                {
                    var RecursiveResult = Satisfies(Antecedents, AnteIter + 1, Given, NextSubstitution);

                    if (RecursiveResult != null)
                        return RecursiveResult;
                }
            }

            return null;
        }
    }
}
